//
//  LPMainTabBarController.swift
//  LPAPP
//
//  Created by lipeng on 2021/3/17.
//

import UIKit


/// 枚举的好处 顺序不用太在意顺序  tabbar配置
enum MainTabbarType: String, CaseIterable {

    case home = "首页"
    case find = "发现"
    case mine = "我的"
    
    // 图片名称
    func descriptionImgName(_ isNormal:Bool = true) -> String {
        return isNormal ? self.rawValue : (self.rawValue + "-Selected")
    }
    // 对应的控制器
    func getCurVC() -> UIViewController {
        switch self {
        case .home:
            return LPHomeViewController()
        case .find:
            return LPHomeViewController()
        case .mine:
            return LPHomeViewController()
        }
    }
}

public class LPMainTabBarController: UITabBarController {

    public override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        initTabBar()
    }
           
    func initTabBar() {
        
        for type in MainTabbarType.allCases {
            let vc = type.getCurVC()
//            let normalImg = type.descriptionImgName
//            let selectedImg = type.descriptionImgName(false)
//            setupChildNavVC(title: normalImg, rootViewController: vc, image: (UIImage(named: normalImg)?.withRenderingMode(.alwaysOriginal))!, selectedImage: (UIImage(named: selectedImg)?.withRenderingMode(.alwaysOriginal))!)
            
            let home = LPMainNavigationController(rootViewController: vc)
            home.tabBarItem.title = type.rawValue
//            home.tabBarItem.image = UIImage(systemName: type.rawValue, withConfiguration: UIImage.SymbolConfiguration(weight: .regular))?.withRenderingMode(.alwaysOriginal)
            addChild(home)
        }
        
        // 设置 tabBar & tabBarItem
        setTabBarItemAttributes(bgColor: UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1))
        
        
//        tabBar.backgroundColor = UIColor.white
//        tabBar.backgroundImage = UIImage()
//        tabBar.shadowImage = UIImage()
//        dropShadow(
//            withOffset: CGSize(width: 0, height: -1),
//            radius: 2,
//            color: UIColor.black,
//            opacity: 0.1)
    }
    
    //创建子控制器
    func setupChildNavVC(title:String,rootViewController vc:UIViewController,image img:UIImage,selectedImage selImg:UIImage) {
        let nav = LPMainNavigationController(rootViewController: vc)
        nav.tabBarItem.title = title
        nav.tabBarItem.selectedImage = selImg
        nav.tabBarItem.image = img
        addChild(nav)
    }
    
    
    /// 灵活设置
    func setTabBarItemAttributes(fontName: String = "Courier",
                                 fontSize: CGFloat = 14,
                                 normalColor: UIColor = .gray,
                                 selectedColor: UIColor = .red,
                                 bgColor: UIColor = .white) {
        // tabBarItem 文字大小
        var attributes: [NSAttributedString.Key: Any] = [.font: UIFont(name: fontName, size: fontSize)!]
        
        // tabBarItem 文字默认颜色
        attributes[.foregroundColor] = normalColor
        UITabBarItem.appearance().setTitleTextAttributes(attributes, for: .normal)
        
        // tabBarItem 文字选中颜色
        attributes[.foregroundColor] = selectedColor
        UITabBarItem.appearance().setTitleTextAttributes(attributes, for: .selected)
        
        // tabBar 文字、图片 统一选中高亮色
        tabBar.tintColor = selectedColor
        
        // tabBar 背景色
        tabBar.barTintColor = bgColor
    }
       
    /// 阴影
    func dropShadow(
        withOffset offset: CGSize,
        radius: CGFloat,
        color: UIColor?,
        opacity: CGFloat
    ) {
        
        let path = CGMutablePath()
        path.addRect(self.tabBar.bounds, transform: .identity)
        tabBar.layer.shadowPath = path
        path.closeSubpath()
    
        tabBar.layer.shadowColor = color?.cgColor
        tabBar.layer.shadowOffset = offset
        tabBar.layer.shadowRadius = radius
        tabBar.layer.shadowOpacity = Float(opacity)
        tabBar.clipsToBounds = false
    }


}
